import { execute } from "../../database/connector";

import { DepartmentQueries } from "../queries/department.query";
import { IDepartment } from "../../models/department.model";

/**
 * gets active departments
 */
export const getDepartments = async () => {
  return execute<IDepartment[]>(DepartmentQueries.GetDepartments, []);
};

/**
 * gets a department based on id provided
 */
export const getDepartmentById = async (id: IDepartment['id']) => {
  return execute<IDepartment>(DepartmentQueries.GetDepartmentById, [id]);
};

/**
 * adds a new active department record
 */
export const insertDepartment = async (department: IDepartment) => {
  const result = await execute<{ affectedRows: number }>(DepartmentQueries.AddDepartment, [
    Object.values(department)
  ]);
  return result.affectedRows > 0;
};

/**
 * updates department information based on the id provided
 */
export const updateDepartment = async (department: IDepartment) => {
  const result = await execute<{ affectedRows: number }>(DepartmentQueries.UpdateDepartmentById, [department, department.id]);
  return result.affectedRows > 0;
};

/**
 * updates department information based on the id provided
 */
 export const deleteDepartment = async (id: IDepartment['id']) => {
  const result = await execute<{ affectedRows: number }>(DepartmentQueries.DeleteDepartmentById, [
    id
  ]);
  return result.affectedRows > 0;
};